package calao.storage;

public abstract class DataWriter 
{

	abstract public void write(int value);
	abstract public void write(long value);
	abstract public void write(double value);
	abstract public void write(String value);
	abstract public void write(boolean value);

	public void write(int[] array)
	{
		write(array.length);
		for (int i=0; i<array.length; i++) write(array[i]);
	}

	public void write(Object[] array)
	{
		write(array.length);
		for (Object val : array) write(val);
	}

	protected void writeBindingName(DataBinding binding)
	{
		write(binding.name);	
	}

	protected void writeBindingVersion(int version)
	{
		write(version);
	}

	@SuppressWarnings("unchecked")
	public void write(Object object)
	{
		if (object==null) write(false);
		else {
			write(true);
			DataBinding binding = DataBinding.getBinding(object.getClass());
			if (binding==null) {
				throw new RuntimeException("No data binding for "+object.getClass());
			}
			writeBindingName(binding); 
			writeBindingVersion(binding.version);
			binding.write(this, object);
		}
	}
	
	public void markChildObjectStart() { }

	public void markChildObjectEnd() { }

}
